package cn.shop.admin.controller;

import cn.shop.admin.entity.dictionaryModule.Dictionary;
import cn.shop.admin.service.DictionaryService;
import cn.shop.base.MyPage;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.constant.Constant;
import cn.shop.utils.enums.OperationType;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 字典表 (Dictionary)表控制层
 *
 * @author jzw
 * @since 2021-11-03 13:07:21
 */
@Validated
@RestController
@RequestMapping("/api/v1/dictionary")
public class DictionaryController {

	/**
	 * 服务对象
	 */
	@Resource
	private DictionaryService dictionaryService;

	/**
	 * 通过code 查询所有子集
	 *
	 * @param code 字典标识
	 * @return cn.shop.utils.ApiResult<java.util.List < java.lang.Object>>
	 * @author jzw
	 * @since 2021/11/4 13:24
	 */
	@GetMapping("queryDicChildrenByCode")
	public ApiResult<List<Dictionary>> queryDicByCode(String code) {
		return ApiResult.success(dictionaryService.queryDicChildrenByCode(code));
	}


	/**
	 * 分页查询所有数据
	 *
	 * @param page       分页对象
	 * @param dictionary 查询实体
	 * @return 所有数据
	 */
	@GetMapping("queryByPage")
	public ApiResult<MyPage<Dictionary>> queryByPage(MyPage<Dictionary> page, Dictionary dictionary,
	                                                 @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime startTime,
	                                                 @RequestParam(required = false) @DateTimeFormat(pattern = Constant.DATETIME_FORMAT) LocalDateTime endTime) {
		return ApiResult.success(dictionaryService.page(page, Wrappers.lambdaQuery(dictionary)
				.apply(startTime != null,
						"date_format (create_time,'%Y-%m-%d %H:%i') >= date_format ({0},'%Y-%m-%d %H:%i')", startTime)
				.apply(endTime != null,
						"date_format (create_time,'%Y-%m-%d %H:%i') <= date_format ({0},'%Y-%m-%d %H:%i')", endTime)));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<Dictionary> selectOne(@PathVariable Serializable id) {
		return ApiResult.success(dictionaryService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param dictionary 实体对象
	 * @return 新增结果
	 */
	@PostMapping("insert")
	@OperationLog(desc = "新增字典", type = OperationType.CREATE)
	public ApiResult<Map<String, Object>> insert(@RequestBody Dictionary dictionary) {
		return ApiResult.result(dictionaryService.save(dictionary));
	}

	/**
	 * 修改数据
	 *
	 * @param dictionary 实体对象
	 * @return 修改结果
	 */
	@PutMapping("update")
	@OperationLog(desc = "修改字典", type = OperationType.UPDATE)
	public ApiResult<Map<String, Object>> update(@RequestBody Dictionary dictionary) {
		return ApiResult.result(dictionaryService.updateById(dictionary));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable Long id) {
		Dictionary dictionary = new Dictionary();
		dictionary.setId(id);
		return ApiResult.result(dictionaryService.removeById(dictionary));
	}

	/**
	 * 批量删除 批量删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 */
	@DeleteMapping("batchDelete")
	public ApiResult<Map<String, Object>> batchDelete(@RequestParam("idList") List<Long> idList) {
		return ApiResult.result(dictionaryService.batchDelete(idList, new Dictionary()));

	}

}

